/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "Animation.h"

#include <iostream>

Animation::Animation(void)
{
    m_numFrames     = 0;
    m_row           = 1;
    m_col           = 1;
    m_currentFrame  = 1;
    m_timeElapsed   = 0;
    
    SetFPS ( 1 );
    // Anim
    m_looping      = true;
    m_pause        = false;
}
 
Animation::Animation(int fps, int row, int amount)
{
    //---
    // Frames
    m_numFrames = amount;

    m_row   = row;
    m_col   = 1;
    m_currentFrame = 1;

    SetFPS( fps );
    
    m_timeElapsed  = 0;
    
    // Anim
    m_looping      = true;
    m_pause        = false;

}


void Animation::Set(int fps, int row, int col, int amount )
{
    m_numFrames = amount;

    m_row   = row;
    m_col   = col;
    m_currentFrame = m_col;

    SetFPS( fps );
}
/**
=============================
Update this animation, move
at the desired FPS
=============================
*/
void Animation::Update(long deltaTimeInMSec)
{
    // if animation is paused,
    // do not update this sprite
    if ( IsPaused() )
        return;

    if ( m_numFrames <= 0 )
        return;

    m_timeElapsed += deltaTimeInMSec;
    /**
     * Check to see if we should
     * update this sprite, base
     * updating on our framesPerSec
     * timer, not CPU time
     */
    if( m_timeElapsed >= m_framesPerMSec )
    {
        int framesToAdvance = m_timeElapsed / m_framesPerMSec;
        m_timeElapsed -= framesToAdvance * m_framesPerMSec;
        
        int desiredFrame = m_currentFrame + framesToAdvance;
        
        // if we are not looping, set to the last 
        // frame
        if ( !IsLooping() && desiredFrame >= m_numFrames ) {
            desiredFrame = m_numFrames - 1;
        }
        // set the new current frame
        SetFrame(desiredFrame);
        
    }


}


Animation::~Animation(void)
{
}
